package yz.cp.back.user.service.impl;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import yz.cp.back.user.service.SerialNumberService;

import java.util.Date;

@Service
public class SerialNumberServiceImpl  implements SerialNumberService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String generate(){
        String date= DateUtil.format(new Date(),"yyyyMMddHHmmSS");
        String key = SERIAL_NUMBER + date;
        long sequence = redisTemplate.opsForValue().increment(key,1);
        if(sequence==0){
            sequence=redisTemplate.opsForValue().increment(key,1);
        }
        String seq = StringUtils.leftPad(String.valueOf(sequence),6,"0");
        StringBuilder sb = new StringBuilder();
        sb.append(date).append(seq);
        String serial = sb.toString();
        return serial;
    }
}
